import {
  selectDeclareList,
  selectDeclareDetail,
  detailsDelivery,
  selectDeliveryList,
  selectFileList,
  // declareOperate,
  saveDeclare,
  selectGoodsList,
  selectContainerList,
  selectContainerByIdsList,
  selectIcCardList,
  getMdsDeliveryInfo,
  selectManifestPlusList,
  selectManifestPlusDetail,
  mdsDeliveryList,
  mdsDelivery,
  mdsSave,
} from '@/services/declare';
import prompt from '@/components/Prompt';

export default {
  namespace: 'declare',

  state: {
    declareList: {}, // 帐单列表
    declareDetail: {},
    detailsDelivery: {},

    deliveryList: {},
    formValues: {},
    goodsList: {},
    containerList: {},

    manifestPlusList:{},
    manifestPlusDetail:{},
    mdsDelivery:{},
    mdsDeliveryList:{},
    
  },

  effects: {
    /**
     * 1.1---查询帐单列表
     */
    *selectDeclareList({ payload, callback }, { call, put }) {
      const response = yield call(selectDeclareList, payload);
      const { code, data } = response;
      if (code !== 0) return;
      const { list, pageSize, total, pageNum } = data;
      yield put({
        type: 'saveAllValus',
        payload: {
          declareList: {
            pagination: {
              current: pageNum,
              pageSize,
              total,
            },
            list,
          },
        },
      });
      callback && callback(list);
    },
    
    /**
     * 1.2查询详情---报关
     */
    *selectDeclareDetail({ payload, callback }, { call, put }) {
      const response = yield call(selectDeclareDetail, payload);
      const { code, data } = response;
      if (code !== 0) return;
      let newData = data;
      const filePayLoad = { bizId: payload.id, fileBizType: 'BILL' };
      const fileResponse = yield call(selectFileList, filePayLoad);
      if (fileResponse.code == 0) {
        newData.fileList = fileResponse.data;
      }
      yield put({
        type: 'saveDetail',
        payload: { declareDetail: { [payload.id]: newData } },
      });
      callback && callback(data);
    },
    // 1.3 明细详情--提运单详情
    *detailsDelivery({ payload, callback }, { call, put }) {
      const response = yield call(detailsDelivery, payload);
      const { code, data } = response;
      if (code !== 0) return;
      let newData = data;
      // newData.fileList = data.attachments || [];

      yield put({
        type: 'saveAllValus',
        payload: { detailsDelivery: { [payload.id]: newData } },
      });
      callback && callback(data);
    },

    //2.1 查询-预备舱单列表
    *selectManifestPlusList({ payload, callback }, { call, put }) {
      const response = yield call(selectManifestPlusList, payload);
      const { code, data } = response;
      if (code !== 0) return;
      const { list, pageSize, total, pageNum } = data;
      yield put({
        type: 'saveAllValus',
        payload: {
          manifestPlusList: {
            pagination: {
              current: pageNum,
              pageSize,
              total,
            },
            list,
          },
        },
      });
      callback && callback(list);
    },
    //2.2 查询详情--预备舱单
    *selectManifestPlusDetail({ payload, callback }, { call, put }) {
      const response = yield call(selectManifestPlusDetail  , payload);
      const { code, data } = response;
      if (code !== 0) return;
      let newData = data;
      const filePayLoad = { bizId: payload.id, fileBizType: 'BILL' };
      const fileResponse = yield call(selectFileList, filePayLoad);
      if (fileResponse.code == 0) {
        newData.fileList = fileResponse.data;
      }
      yield put({
        type: 'saveAllValus',
        payload: { manifestPlusDetail: { [payload.id]: newData } },
      });
      callback && callback(data);
    },
    //2.3 预备舱单--提运单列表
    *mdsDeliveryList({ payload, callback }, { call, put }) {
      const response = yield call(mdsDeliveryList, payload);
      const { code, data } = response;
      if (code !== 0) return;
      const { list, pageSize, total, pageNum } = data;
      const infoList = {
        pagination: {
          current: pageNum,
          pageSize,
          total,
        },
        list,
      };
      // console.log('===payload===', payload);
      yield put({
        type: 'saveMdsDelivery',
        payload: { mdsDeliveryList: { [payload.roadManifestId]: infoList } },
      });
      callback && callback(list);
    },
    
    // 2.4 明细详情--提运单详情
    *mdsDelivery({ payload, callback }, { call, put }) {
      const response = yield call(mdsDelivery, payload);
      const { code, data } = response;
      if (code !== 0) return;
      let newData = data;
      // newData.fileList = data.attachments || [];

      yield put({
        type: 'saveAllValus',
        payload: { mdsDelivery: { [payload.id]: newData } },
      });
      callback && callback(data);
    },
    //2.5 预配舱单--提交
    *mdsSave({ payload, callback }, { call }) {
      const response = yield call(mdsSave, payload);
      const { code, data, message } = response;
      const content = `${payload.flag ? '暂存并申报' : '提交'}${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback(true);
    },

    *saveDeclare({ payload, callback }, { call }) {
      const response = yield call(saveDeclare, payload);
      const { code, data, message } = response;
      const content = `${payload.flag ? '暂存并申报' : '提交'}${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback(true);
    },
    /**
     * 查询明细详情---提运单列表
     */
    *selectDeliveryList({ payload, callback }, { call, put }) {
      const response = yield call(selectDeliveryList, payload);
      const { code, data } = response;
      if (code !== 0) return;
      const { list, pageSize, total, pageNum } = data;
      const infoList = {
        pagination: {
          current: pageNum,
          pageSize,
          total,
        },
        list,
      };
      // console.log('===payload===', payload);
      yield put({
        type: 'saveDetailDelivery',
        payload: { deliveryList: { [payload.roadManifestId]: infoList } },
      });
      callback && callback(list);
    },

    /**
     * 查询商品项列表
     */
    *selectGoodsList({ payload, callback }, { call, put }) {
      const response = yield call(selectGoodsList, payload);
      const { code, data } = response;
      if (code !== 0) return;
      const { list, pageSize, total, pageNum } = data;
      const infoList = {
        pagination: {
          current: pageNum,
          pageSize,
          total,
        },
        list,
      };
      yield put({
        type: 'saveAllValus',
        payload: { goodsList: { [payload.deliveryId]: infoList } },
      });
      callback && callback(list);
    },

    /**
     * 查询集装箱列表
     */
    *selectContainerList({ payload, callback }, { call, put }) {
      const response = yield call(selectContainerList, payload);
      const { code, data } = response;
      if (code !== 0) return;
      const { list, pageSize, total, pageNum } = data;
      const infoList = {
        pagination: {
          current: pageNum,
          pageSize,
          total,
        },
        list,
      };
      yield put({
        type: 'saveAllValus',
        payload: { containerList: { [payload.deliveryId]: infoList } },
      });
      callback && callback(list);
    },
    // 舱单-->提运单-->查询集装箱集合
    *selectContainerByIdsList({ payload, callback }, { call, put }) {
      const response = yield call(selectContainerByIdsList, payload);
      const { code, data } = response;
      if (code !== 0) return;
      callback && callback(data);
    },
    *getMdsDeliveryInfo({ payload, callback }, { call, put }) {
      const response = yield call(getMdsDeliveryInfo, payload);
      const { code, data } = response;
      if (code !== 0) return;
      callback && callback(data);
    },
    //查询IC卡：
    *selectIcCardList({ payload, callback }, { call, put }) {
      const response = yield call(selectIcCardList, payload);
      const { code, data } = response;
      if (code !== 0) return;
      callback && callback(data.list);
    },

    /**
     * 新增编辑帐单
     */
    // *declareOperate({ payload, callback }, { call }) {
    //   const response = yield call(declareOperate, payload);
    //   const { code, data, message } = response;
    //   const content = payload.id ? `编辑${message}` : `新增${message}`;
    //   if (code !== 0) return;
    //   prompt({ content });
    //   callback && callback(data);
    // },

    /**
     * 设置表单参数
     */
    *allValus({ payload }, { _, put }) {
      yield put({
        type: 'saveAllValus',
        payload,
      });
    },
  },

  reducers: {
    //设置表单参数
    saveAllValus(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    saveDetail(state, { payload }) {
      return {
        ...state,
        declareDetail: { ...state.declareDetail, ...payload.declareDetail },
      };
    },
    saveDetailDelivery(state, { payload }) {
      // console.log('=======', state, payload);
      return {
        ...state,
        deliveryList: { ...state.deliveryList, ...payload.deliveryList },
      };
    },
    saveMdsDelivery(state, { payload }) {
      // console.log('=======', state, payload);
      return {
        ...state,
        mdsDeliveryList: { ...state.mdsDeliveryList, ...payload.mdsDeliveryList },
      };
    },
  },
};
